package com.huxi.myapplication.slice;

import com.huxi.myapplication.CityModel;
import com.huxi.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.global.resource.Resource;
import ohos.javax.xml.parsers.ParserConfigurationException;
import ohos.javax.xml.parsers.SAXParser;
import ohos.javax.xml.parsers.SAXParserFactory;
import ohos.org.xml.sax.Attributes;
import ohos.org.xml.sax.SAXException;
import ohos.org.xml.sax.SAXParseException;
import ohos.org.xml.sax.helpers.DefaultHandler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    public static final String RESOURCE_NAME = "resources/rawfile/city_data.xml";
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        parse();
    }
    List<CityModel> provinceList =new ArrayList();
    StringBuffer sb = new StringBuffer();
    private void parse() {

        try {
          Resource resource = getResourceManager().getRawFileEntry(RESOURCE_NAME).openRawFile();
            SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
            saxParser.parse(resource,new DefaultHandler(){
//                List<CityModel> provinceList =new ArrayList();
                CityModel provinceModel = new CityModel();
                CityModel cityModel =new CityModel();
                CityModel countyModel =new CityModel();

                        @Override
                        public void startDocument() throws SAXException {
                            super.startDocument();
                            System.out.println("start doc");

                        }

                        @Override
                        public void endDocument() throws SAXException {
                            super.endDocument();
                            System.out.println("end doc"+sb.toString());
                        }

                        @Override
                        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                            super.startElement(uri, localName, qName, attributes);
                            System.out.println("startElement"+localName+"qname: "+qName);
                            if(qName.startsWith("province")){
                                sb.append("省份："+attributes.getValue(0)+attributes.getValue(1));
                                provinceModel =new  CityModel();
                                provinceModel.cityName = attributes.getValue(0);
                                provinceModel.cityCode = attributes.getValue(1);
                                provinceModel.cityType = CityModel.unitType.TYPE_PROVINCE;
                                provinceModel.countyList =new  ArrayList();
                            }else if(qName.startsWith("city")){
                                //可以调用XmlPullParser的nextText()方法来获取节点的值
                                sb.append("	市州："+attributes.getValue(0)+	attributes.getValue(1));
                                cityModel =new CityModel();
                                cityModel.cityName = attributes.getValue(0);
                                cityModel.cityCode = attributes.getValue(1);
                                cityModel.cityType = CityModel.unitType.TYPE_CITY;
                                cityModel.countyList = new ArrayList();
                            }else if(qName.startsWith("county")){
                                sb.append("区县："+attributes.getValue(0)+attributes.getValue(1));

                                countyModel =new CityModel();
                                countyModel.cityName =  attributes.getValue(0);
                                countyModel.cityCode = attributes.getValue(1);
                                countyModel.cityType = CityModel.unitType.TYPE_COUNTY;
                            }


                        }

                        @Override
                        public void endElement(String uri, String localName, String qName) throws SAXException {
                            super.endElement(uri, localName, qName);
                            System.out.println("endElement"+localName+"qname: "+qName);
                            if(qName.startsWith("province")){
                                provinceList.add(provinceModel);
                            }else if(qName.startsWith("city")){
                                List<CityModel>  temp = provinceModel.countyList;
                                if (temp!=null) {
                                    temp.add(cityModel);
                                }
                                provinceModel.countyList = temp;
                            }else if(qName.startsWith("county")){
                                List<CityModel> temp = provinceModel.countyList;
                                if (temp!=null) {
                                    temp.add(countyModel);
                                }
                                cityModel.countyList = temp;
                            }

                        }

                        @Override
                        public void characters(char[] ch, int start, int length) throws SAXException {
                            super.characters(ch, start, length);
                        }

                        @Override
                        public void error(SAXParseException e) throws SAXException {
                            super.error(e);
                        }

                        @Override
                        public void fatalError(SAXParseException e) throws SAXException {
                            super.fatalError(e);
                        }
                    }

            );

            resource.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }

        System.out.println("mmss: "+ provinceList.size());
        ((Text)findComponentById(ResourceTable.Id_text_helloworld)).setText(sb.toString());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
